/**
 * 
 */
package com.res.simulator.handler;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import com.res.common.handler.CommonHandler;
import com.res.simulator.model.RecordAndPlayModel;
import com.res.simulator.view.SearchResultView;
import com.res.simulator.plugins.MessageCommunicationClientPlugin;
import com.res.simulator.transformer.RecorAndPalySearchResultTransformer;
import com.res.system.utils.SimulatorUtilities;
import com.res.system.utils.SimulatorUtilities.ConfigData;
import com.sip.comserver.communication.CommunicationMessage;

/**
 * @author mg40327
 *
 */
public class RecordAndPlaySearchResultHandler implements CommonHandler<List<SearchResultView>, RecordAndPlayModel>{

	 private static final Logger logger = Logger.getLogger(RecordAndPlaySearchResultHandler.class);
	

	public List<SearchResultView> process(RecordAndPlayModel model) throws Exception {

		final RecorAndPalySearchResultTransformer restTransformer = new RecorAndPalySearchResultTransformer();
		final Map<String, Object> domainMap = restTransformer.transformModelToDomian(model);

		final Map<String, Object> returnMap = commit(domainMap);
		final List<SearchResultView> resultViewList = restTransformer.transformDomianToView(returnMap);
		return resultViewList;

	}

	private Map<String, Object> commit(final Map<String, Object> domainMap) throws IOException, ClassNotFoundException {
		
			final CommunicationMessage message = new CommunicationMessage();

	        final String serverAddress = SimulatorUtilities.getConfigData(ConfigData.SERVER_ADDRESS);
	        final String port = SimulatorUtilities.getConfigData(ConfigData.SERVER_PORT);

	        logger.info("Message Header==>" + "JOBType:cm" + "|ID:" + serverAddress + "|Port:" + port + "|Executor:RecordAndPlaySearchExecutor");

	        message.setMessageHeader("JOBType:APISubmitCall|Executor:RecordAndPlaySearchExecutor|ID:" + serverAddress + "|Port:" + port);

	        message.setObject(domainMap);

	        final MessageCommunicationClientPlugin plugin = new MessageCommunicationClientPlugin();
	        final CommunicationMessage returnedMessage = plugin.invoke(message);
	        
	  
	        final Map<String, Object> returnMap = (Map<String, Object>) returnedMessage.getObject();
	        
	        return returnMap;
	}
	
	

}
